
var apiHost = wx.getStorageSync('apiHost');
var yunId = wx.getStorageSync('yunId');
// var openId = wx.getStorageSync('openId');
var userId = wx.getStorageSync('userId');

var goodsType = "jd";
var orderType_z = "zonghe";         //综合排序
var orderType_t = "totalSalesDesc"; //销量排序
var orderType_c = "couponMoneyDesc";   //优惠券排序

Page({

  /**
   * 页面的初始数据
   */
  data: {
    jdGoodsList: [],       //京东商品集合
    JdListLength: '10',      //京东商品集合长度
    JdListStart: '0',      //京东商品集合开始
    orderTypeNum: '1',         //京东商品排序方式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;

    //获取页面跳转过来的参数（搜索值）
    var searVal = options.searVal;    
    that.setData({
      searchValue: searVal
    })

    var length = that.data.JdListLength;          //查询长度
    var Start = that.data.JdListStart;            //查询开始
    var orderNum = that.data.orderTypeNum;        //点击
    var orderType = that.getOrderTyep(orderNum);  //筛选条件

    console.log(searVal)

    that.jdGoodsHeght();                //京东优惠商品高度
    that.orderGoodsList(Start, length, orderType,searVal); //京东优惠商品list
  },

  //获取排序方式
  getOrderTyep: function (orderNum) {
    switch (orderNum) {
      case '1':
        return orderType_z;
        break;
      case '2':
        return orderType_t;
        break;
      case '3':
        return orderType_c;
        break;
    }
  },
  /**
   * 获取京东商品集合
   */
  getJdGoodsList: function (start, length, orderTypea, keyword) {
    var that = this;
    
    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/goods/list",
      data: {
        // 'type': goodsType,
        start: start,
        length: length,
        orderType: orderTypea,
        keyword: keyword
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg
        if (code == "10000") {
          var jdList = res.data.data.list;
          var jdGoodsListss = that.data.jdGoodsList;

          // 追加数据
          for (var i = 0; i < jdList.length; i++) {
            jdGoodsListss.push(jdList[i])
          }

          that.setData({
            jdGoodsList: jdGoodsListss
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },
  //获取高度
  jdGoodsHeght: function () {
    var that = this;
    /*获取指定元素高度*/
    var query2 = wx.createSelectorQuery();
    var query3 = wx.createSelectorQuery();
    var searchHHeight = "";
    var sortCon = "";

    query3.select('#searchH').boundingClientRect()
    query3.exec(function (res) {
      searchHHeight = res[0].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            jdGoodsHeght: res.windowHeight - searchHHeight
          });
        }
      });
    });
  },

  //获取搜索框输入内容
  searchInput: function (e) {
    var that = this;
    that.setData({
      searchValue: e.detail.value
    })
  },

  //点击搜索按钮
  searchSubmit: function () {
    var that = this;
    var searVal = that.data.searchValue;    //获取搜索框内容

    var length = that.data.JdListLength;          //查询长度
    var Start = that.data.JdListStart;            //查询开始
    var orderNum = that.data.orderTypeNum;        //点击
    var orderType = that.getOrderTyep(orderNum);  //筛选条件

    that.orderGoodsList(Start, length, orderType, searVal); //京东优惠商品list

  },

  //排序
  roderP: function (e) {
    var that = this;
    var orderT = e.target.dataset.toggle;
    var lengthflag = that.data.JdListLength;
    var startFlag = that.data.JdListStart;
    var searVal = that.data.searchValue;    //获取搜索框内容

    if (startFlag == "0") {
      startFlag = '1';
    }

    var length = parseInt(lengthflag) * parseInt(startFlag);
    that.setData({
      orderTypeNum: orderT
    })

    var orderType = that.getOrderTyep(orderT);

    that.orderGoodsList('0', length, orderType, searVal); //京东优惠商品list

  },

  //排序京东商品列表
  orderGoodsList: function (start, length, orderType, keyword) {
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/goods/list",
      data: {
        // 'type': goodsType,
        start: start,
        length: length,
        orderType: orderType,
        keyword: keyword
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {

          var jdList = res.data.data.list;

          var flag = false
          if (jdList.length == 0) {
            flag = true;
          } else {
            flag = false;
          }

          that.setData({
            jdGoodsList: jdList,
            is_none: flag
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }


        wx.hideLoading();   //隐藏加载loading

      }
    })
  },

  //滚动到底部触发事件  
  searchScrollLower: function () {
    var that = this;
    var length = that.data.JdListLength;
    var startFlag = that.data.JdListStart;
    var orderT = that.data.orderTypeNum;
    var searVal = that.data.searchValue;    //获取搜索框内容

    var nowStart = parseInt(startFlag) + 1

    var orderType = that.getOrderTyep(orderT);

    that.setData({
      JdListStart: nowStart
    });

    that.getJdGoodsList(nowStart, length, orderType, searVal); //京东优惠商品list

  },
  //跳转商品详情
  goJdDetail: function (e) {
    var that = this;
    var spuId = e.currentTarget.dataset.toggle;

    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: '../jdDetails/jdDetails?spuId=' + spuId,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})